package
{

    import demo.DemoViewport;

    import edit.EditViewport;

    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.PNGEncoderOptions;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
    import flash.system.ApplicationDomain;
    import flash.utils.ByteArray;

    public class ScaleImgEditor extends Sprite
    {
        private var mGUILoader:Loader;
        private var mGUIMc:MovieClip;

        private var mEditViewport:EditViewport;

        private var mDemoViewport:DemoViewport;

        private var mSaveBtn:Sprite;
        private var mSaveFolder:File;

        public function ScaleImgEditor()
        {
            mGUILoader = new Loader();
            mGUILoader.load(new URLRequest("GUI.swf"));
            mGUILoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadGUIComplete);
        }

        private function onLoadGUIComplete(event:Event):void
        {
            var domain:ApplicationDomain = mGUILoader.contentLoaderInfo.applicationDomain;
            var GUIClass:Class = domain.getDefinition("GUI") as Class;
            mGUIMc = new GUIClass();

            this.stage.scaleMode = StageScaleMode.NO_SCALE;
            this.stage.align = StageAlign.TOP_LEFT;
            this.stage.stageWidth = mGUIMc.width;
            this.stage.stageHeight = mGUIMc.height;
            this.stage.color = 0x454545;

            this.addChild(mGUIMc);

            initialize();
        }

        //==================================
        //== Initialize & Dispose
        //==================================

        private function initialize():void
        {
            mEditViewport = new EditViewport();
            mEditViewport.initialize(mGUIMc, onBitmapTextureUpdate);

            mDemoViewport = new DemoViewport();
            mDemoViewport.initialize(mGUIMc);

            mSaveBtn = mGUIMc["saveBtnMc"];
            mSaveBtn.addEventListener(MouseEvent.MOUSE_UP, onClickSaveBtn);
        }

        //==================================
        //== Private Function
        //==================================

        private function onBitmapTextureUpdate(_topLeft:BitmapData,
                                               _topRight:BitmapData,
                                               _bottomLeft:BitmapData,
                                               _bottomRight:BitmapData,
                                               _middle:BitmapData,
                                               _topMiddle:BitmapData,
                                               _leftMiddle:BitmapData,
                                               _rightMiddle:BitmapData,
                                               _bottomMiddle:BitmapData):void
        {
            mDemoViewport.onBitmapTextureUpdate(_topLeft, _topRight, _bottomLeft, _bottomRight,
                                                _middle,
                                                _topMiddle, _leftMiddle, _rightMiddle, _bottomMiddle);
        }

        private function onClickSaveBtn(e:MouseEvent):void
        {
            if (mEditViewport.topLeft != null)
            {
                mSaveFolder = new File();
                mSaveFolder.addEventListener(Event.SELECT, onFolderSelected);
                mSaveFolder.browseForDirectory("Choose a directory");
            }
        }

        private function onFolderSelected(e:Event):void
        {
            doSaveImage(mEditViewport.topLeft, "topLeft.png");
            doSaveImage(mEditViewport.topRight, "topRight.png");
            doSaveImage(mEditViewport.bottomLeft, "bottomLeft.png");
            doSaveImage(mEditViewport.bottomRight, "bottomRight.png");
            doSaveImage(mEditViewport.middle, "middle.png");
            doSaveImage(mEditViewport.topMiddle, "topMiddle.png");
            doSaveImage(mEditViewport.leftMiddle, "leftMiddle.png");
            doSaveImage(mEditViewport.rightMiddle, "rightMiddle.png");
            doSaveImage(mEditViewport.bottomMiddle, "bottomMiddle.png");
        }

        private function doSaveImage(_bitmapData:BitmapData, _fileName:String):void
        {
            var saveFile:File = mSaveFolder.resolvePath(_fileName);

            //编码为PNG格式
            var imgByteArray:ByteArray = _bitmapData.encode(new Rectangle(0, 0, _bitmapData.width, _bitmapData.height),
                                                            new PNGEncoderOptions(true));
            //存储到本地
            var fileStream:FileStream = new FileStream();
            fileStream.open(saveFile, FileMode.WRITE);
            fileStream.writeBytes(imgByteArray);
            fileStream && fileStream.close();
            fileStream = null;
        }

    }
}
